package com.cccyy.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

public class QRCodeUtil {
    private static final String CHARSET = "UTF-8";//字符集格式
    private static final String FORMAT_NAME = "JPG";//图片格式
    private static final int QRCODE_SIZE = 300;//二维码尺寸
    private static final int WIDTH = 60;//LOGO宽度
    private static final int HEIGHT = 60;//LOGO高度
    private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class);

    public static void main(String[] args) throws IOException, WriterException {
        BufferedImage image = genQRImg("测试这个方法");//可以通过二维码获取到这个信息
        File file = new File("D:\\WorkSpace\\Study\\java_Utils\\study-test\\src\\main\\resources\\QRImg\\QRIMG.jpg");
        log.info("测试下在这里打印日志");
        ImageIO.write(image, FORMAT_NAME, file);
    }

    /**
     * 生成二维码图片
     */
    public static BufferedImage genQRImg(String info) throws WriterException, IOException {
        Hashtable<EncodeHintType, Object> hashtable = new Hashtable<>();
        hashtable.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hashtable.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hashtable.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(info, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hashtable);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
//        insertLogo(image, "logo.jpg", true);
        return image;
    }

    private static void insertLogo(BufferedImage image, String imagePath, boolean compress) throws IOException {
        File file = new File(imagePath);
        if (!file.exists()) {
            log.error("文件不存在");
            return;
        }
        Image src = ImageIO.read(file);
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        //压缩
        if (compress) {
            if (width > WIDTH) {
                width = WIDTH;
            }
            if (height > HEIGHT) {
                height = HEIGHT;
            }
            Image imageTmp = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics graphics = tag.getGraphics();
            //绘制缩小后的图片
            graphics.drawImage(imageTmp, 0, 0, null);
            graphics.dispose();
            src = imageTmp;
        }
        //插入LOGO
        Graphics2D graphics = image.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graphics.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, height, 6, 6);
        graphics.setStroke(new BasicStroke(3f));
        graphics.draw(shape);
        graphics.dispose();
    }
}
